/**
 * Copyright (c) 2011-2012
 * OmniCore CodeGuide
 */
package de.tum.in.oose.bumpers.control;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import de.tum.in.oose.bumpers.cars.Car;

/**
 * Faster car wins.
 * @author ivanst
 *
 */
public class FasterCarWinsCollision extends CollisionStrategy {

	@Override
	public Car detectCollision(Car drivenCar, Car roboCar) {
		Point     p1 = drivenCar.getPosition( );
		Dimension d1 = drivenCar.getSize( );
		Rectangle r1 = new Rectangle(p1, d1);
		r1.translate(p1.x / 8, p1.y / 8);
		r1.grow(-1 * d1.width / 4, -1 * d1.height / 4);
		
		Point     p2 = roboCar.getPosition( );
		Dimension d2 = roboCar.getSize( );
		Rectangle r2 = new Rectangle(p2, d2);
		r2.translate(p2.x / 8, p2.y / 8);
		r2.grow(-1 * d2.width / 4, -1 * d2.height / 4);
		
		if (r1.intersects(r2))
		{
			if (drivenCar.getSpeed() > roboCar.getSpeed()) {
				return drivenCar;
			} 
			else {
				return roboCar;
			}
		}
		return null;
	}

	@Override
	public String getName() {
		return "Faster car wins collision";
	}

}
